package com.sos.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sos.widget.R;

import java.util.ArrayList;
import java.util.List;

public class StarListView extends FrameLayout {
    private int maxStar = 5;
    private float startSize = 14;
    private int startSpace = 4;
    private int drawableStar = R.drawable.sel_start;
    private LinearLayout linearLayout;
    private List<ImageView> starList;

    public StarListView(@NonNull Context context) {
        super(context);
    }

    public StarListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }


    public void initView(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.StarListView);
        if (typedArray != null) {
            maxStar = typedArray.getInt(R.styleable.StarListView_maxStar, maxStar);
            startSize = typedArray.getDimension(R.styleable.StarListView_starSize, startSize);
            startSpace = (int) typedArray.getDimension(R.styleable.StarListView_starSpace, startSpace);
            drawableStar = (int) typedArray.getResourceId(R.styleable.StarListView_starDrawable, R.drawable.sel_start);
        }
        linearLayout = new LinearLayout(getContext());
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        starList = new ArrayList<>();
        for (int i = 0; i < maxStar; i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) startSize, (int) startSize);
            params.setMargins(0, 0, startSpace, 0);
            ImageView start = new ImageView(getContext());
            start.setLayoutParams(params);
            start.setImageResource(drawableStar);
            int finalI = i;
            start.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (startListener != null) {
                        setStar(finalI + 1);
                    }
                }
            });
            starList.add(start);
            linearLayout.addView(start);
        }
        addView(linearLayout);
    }

    public void setStar(int star) {
        if (star > maxStar) {
            star = maxStar;
        }
        for (int i = 0; i < starList.size(); i++) {
            if (i + 1 <= star) {
                if (!starList.get(i).isSelected()) {
                    starList.get(i).setSelected(true);
                }
            } else {
                if (starList.get(i).isSelected()) {
                    starList.get(i).setSelected(false);
                }
            }
        }
        if (startListener != null) {
            startListener.startCallBack(star);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public StartListener startListener;

    public void setStartListener(StartListener startListener) {
        this.startListener = startListener;
    }

    public interface StartListener {
        void startCallBack(int star);
    }
}
